﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using Hi_Baidu.ProcessModel;

namespace Hi_Baidu.Timer
{
    /// <summary>
    ///     提供了添加分组的超时计算与相关操作
    /// </summary>
    class AddItemTimeOutEvent : TimeOutEvent
    {
        private delegate void RETURN_ITEM(String ItemKey);
        private byte[] username;
        private byte[] ItemName;
        private byte[] ItemKey;

        /// <summary>
        ///     提供了添加分组的超时计算与相关操作
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="ItemName">新分组名</param>
        /// <param name="Key">唯一表示</param>
        /// <param name="Interval">超时器触发间隔</param>
        /// <param name="TryCount">总共尝试次数</param>
        /// <param name="ItemKey">新用户分组的KEY</param>
        public AddItemTimeOutEvent(byte[] username, byte[] ItemName, byte[] ItemKey, String Key, int Interval, int TryCount) : base(Key, Interval, TryCount)
        {
            this.username = username;
            this.ItemName = ItemName;
            this.ItemKey = ItemKey;
            this.OnTryFull += new EventHandler<EventArgs>(AddItemTimeOutEvent_OnTryFull);
            this.OnTick += new EventHandler<EventArgs>(AddItemTimeOutEvent_OnTick);
        }

        void AddItemTimeOutEvent_OnTick(object sender, EventArgs e)
        {
            //重试添加分组
            Hi_Baidu.ProcessModel.UpdateProcessModel.AddItem(this.username, this.ItemName, this.ItemKey);
        }

        void AddItemTimeOutEvent_OnTryFull(object sender, EventArgs e)
        {
            System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                                                 new RETURN_ITEM(FriendTreeViewModel.ReturnItem),
                                                                 this.ItemKey);
            System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new PUBLIC_DELEGATE(Globle.ShowWindow));
        }
    }
}
